package com.ruoyi.web.controller.tool;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.system.dto.PayloadDTO;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;

/**
 * swagger 用户测试方法
 * 
 * @author ruoyi
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController extends BaseController
{
    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
    {
        users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
        users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
    }

    @PostMapping("/webhook-endpoint")
    public String handleWebhook(@RequestBody String payload) {
        System.out.println("Received webhook payload: " + payload);
        // 解析payload并处理MR事件
        payload = "{\"object_kind\":\"merge_request\",\"event_type\":\"merge_request\",\"user\":{\"id\":1,\"name\":\"Administrator\",\"username\":\"root\",\"avatar_url\":\"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon\",\"email\":\"[REDACTED]\"},\"project\":{\"id\":2,\"name\":\"ruoyi\",\"description\":\"ruoyi\",\"web_url\":\"http://localhost:8200/root/ruoyi\",\"avatar_url\":null,\"git_ssh_url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"git_http_url\":\"http://localhost:8200/root/ruoyi.git\",\"namespace\":\"Administrator\",\"visibility_level\":20,\"path_with_namespace\":\"root/ruoyi\",\"default_branch\":\"main\",\"ci_config_path\":null,\"homepage\":\"http://localhost:8200/root/ruoyi\",\"url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"ssh_url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"http_url\":\"http://localhost:8200/root/ruoyi.git\"},\"object_attributes\":{\"assignee_id\":1,\"author_id\":1,\"created_at\":\"2024-06-10 01:26:41 UTC\",\"description\":\"Uat\",\"head_pipeline_id\":null,\"id\":2,\"iid\":2,\"last_edited_at\":null,\"last_edited_by_id\":null,\"merge_commit_sha\":null,\"merge_error\":null,\"merge_params\":{\"force_remove_source_branch\":\"1\"},\"merge_status\":\"preparing\",\"merge_user_id\":null,\"merge_when_pipeline_succeeds\":false,\"milestone_id\":null,\"source_branch\":\"uat\",\"source_project_id\":2,\"state_id\":1,\"target_branch\":\"main\",\"target_project_id\":2,\"time_estimate\":0,\"title\":\"Uat\",\"updated_at\":\"2024-06-10 01:26:41 UTC\",\"updated_by_id\":null,\"url\":\"http://localhost:8200/root/ruoyi/-/merge_requests/2\",\"source\":{\"id\":2,\"name\":\"ruoyi\",\"description\":\"ruoyi\",\"web_url\":\"http://localhost:8200/root/ruoyi\",\"avatar_url\":null,\"git_ssh_url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"git_http_url\":\"http://localhost:8200/root/ruoyi.git\",\"namespace\":\"Administrator\",\"visibility_level\":20,\"path_with_namespace\":\"root/ruoyi\",\"default_branch\":\"main\",\"ci_config_path\":null,\"homepage\":\"http://localhost:8200/root/ruoyi\",\"url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"ssh_url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"http_url\":\"http://localhost:8200/root/ruoyi.git\"},\"target\":{\"id\":2,\"name\":\"ruoyi\",\"description\":\"ruoyi\",\"web_url\":\"http://localhost:8200/root/ruoyi\",\"avatar_url\":null,\"git_ssh_url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"git_http_url\":\"http://localhost:8200/root/ruoyi.git\",\"namespace\":\"Administrator\",\"visibility_level\":20,\"path_with_namespace\":\"root/ruoyi\",\"default_branch\":\"main\",\"ci_config_path\":null,\"homepage\":\"http://localhost:8200/root/ruoyi\",\"url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"ssh_url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"http_url\":\"http://localhost:8200/root/ruoyi.git\"},\"last_commit\":{\"id\":\"a39deeac9763e89f81993bf85cc5f13efb9c3bad\",\"message\":\"error\\n\",\"title\":\"error\",\"timestamp\":\"2024-06-10T08:16:19+08:00\",\"url\":\"http://localhost:8200/root/ruoyi/-/commit/a39deeac9763e89f81993bf85cc5f13efb9c3bad\",\"author\":{\"name\":\"luomao1\",\"email\":\"luomao1@lenovo.com\"}},\"work_in_progress\":false,\"total_time_spent\":0,\"time_change\":0,\"human_total_time_spent\":null,\"human_time_change\":null,\"human_time_estimate\":null,\"assignee_ids\":[1],\"state\":\"opened\",\"blocking_discussions_resolved\":true,\"action\":\"open\"},\"labels\":[],\"changes\":{\"merge_status\":{\"previous\":\"unchecked\",\"current\":\"preparing\"}},\"repository\":{\"name\":\"ruoyi\",\"url\":\"ssh://git@localhost:8200/root/ruoyi.git\",\"description\":\"ruoyi\",\"homepage\":\"http://localhost:8200/root/ruoyi\"},\"assignees\":[{\"id\":1,\"name\":\"Administrator\",\"username\":\"root\",\"avatar_url\":\"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon\",\"email\":\"[REDACTED]\"}]}";
        
        // 假设你已经处理了事件并决定提交评论
        PayloadDTO object = JSON.parseObject(payload, PayloadDTO.class);
        

        // 调用GitLab API提交评论

        return "Webhook received and processed";
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public R<List<UserEntity>> userList()
    {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        return R.ok(userList);
    }

    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
    @GetMapping("/{userId}")
    public R<UserEntity> getUser(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            return R.ok(users.get(userId));
        }
        else
        {
            return R.fail("用户不存在");
        }
    }

    @ApiOperation("新增用户")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", dataTypeClass = String.class),
        @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", dataTypeClass = String.class),
        @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/save")
    public R<String> save(UserEntity user)
    {
        String url = "";
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return R.fail("用户ID不能为空");
        }
        users.put(user.getUserId(), user);
        return R.ok();
    }

    @ApiOperation("更新用户")
    @PutMapping("/update")
    public R<String> update(@RequestBody UserEntity user)
    {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return R.fail("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId()))
        {
            return R.fail("用户不存在");
        }
        users.remove(user.getUserId());
        users.put(user.getUserId(), user);
        return R.ok();
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
    @DeleteMapping("/{userId}")
    public R<String> delete(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            users.remove(userId);
            return R.ok();
        }
        else
        {
            return R.fail("用户不存在");
        }
    }
}

@ApiModel(value = "UserEntity", description = "用户实体")
class UserEntity
{
    @ApiModelProperty("用户ID")
    private Integer userId;

    @ApiModelProperty("用户名称")
    private String username;

    @ApiModelProperty("用户密码")
    private String password;

    @ApiModelProperty("用户手机")
    private String mobile;

    public UserEntity()
    {

    }

    public UserEntity(Integer userId, String username, String password, String mobile)
    {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.mobile = mobile;
    }

    public Integer getUserId()
    {
        return userId;
    }

    public void setUserId(Integer userId)
    {
        this.userId = userId;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getMobile()
    {
        return mobile;
    }

    public void setMobile(String mobile)
    {
        this.mobile = mobile;
    }
}
